package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.accountmanage.entity.entityVo.UserMenuVo;
import com.antdesignbackend.backend.accountmanage.service.UserMenuService;
import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processlist.service.ProcessListService;
import com.antdesignbackend.backend.activiti.processrecord.entity.Processrecord;
import com.antdesignbackend.backend.activiti.processrecord.entity.entityVo.ProcessrecordVo;
import com.antdesignbackend.backend.activiti.processrecord.service.ProcessrecordService;
import com.antdesignbackend.backend.activiti.processstay.entity.Processstay;
import com.antdesignbackend.backend.activiti.processstay.service.ProcessstayService;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.reviewer.entity.entityVo.ReviewerVo;
import com.antdesignbackend.backend.activiti.reviewer.service.ReviewerService;
import com.antdesignbackend.backend.activiti.util.ActivitiUtil;
import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.CalendarDate;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.entity.*;
import com.antdesignbackend.backend.entity.entityVo.*;
import com.antdesignbackend.backend.form.animalApplication.entity.AnimalApplication;
import com.antdesignbackend.backend.form.animalApplication.service.AnimalApplicationService;
import com.antdesignbackend.backend.form.animalDaily.entity.AnimalDaily;
import com.antdesignbackend.backend.form.animalDaily.service.AnimalDailyService;
import com.antdesignbackend.backend.form.animalOrigin.entity.AnimalOrigin;
import com.antdesignbackend.backend.form.animalOrigin.service.AnimalOriginService;
import com.antdesignbackend.backend.form.businessAccount.entity.BusinessAccount;
import com.antdesignbackend.backend.form.businessAccount.service.BusinessAccountService;
import com.antdesignbackend.backend.form.certificateA.entity.CertificateA;
import com.antdesignbackend.backend.form.certificateA.service.CertificateAService;
import com.antdesignbackend.backend.form.certificateB.entity.CertificateB;
import com.antdesignbackend.backend.form.certificateB.service.CertificateBService;
import com.antdesignbackend.backend.form.farmersDaily.entity.FarmersDaily;
import com.antdesignbackend.backend.form.farmersDaily.service.FarmersDailyService;
import com.antdesignbackend.backend.form.investors.entity.Investors;
import com.antdesignbackend.backend.form.investors.service.InvestorsService;
import com.antdesignbackend.backend.form.pigAccount.entity.PigAccount;
import com.antdesignbackend.backend.form.pigAccount.service.PigAccountService;
import com.antdesignbackend.backend.form.report.entity.Report;
import com.antdesignbackend.backend.form.report.service.ReportService;
import com.antdesignbackend.backend.form.saleDeclarationA.entity.SaleDeclarationA;
import com.antdesignbackend.backend.form.saleDeclarationA.service.SaleDeclarationAService;
import com.antdesignbackend.backend.form.saleDeclarationB.entity.SaleDeclarationB;
import com.antdesignbackend.backend.form.saleDeclarationB.service.SaleDeclarationBService;
import com.antdesignbackend.backend.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/home")
@Slf4j
public class HomeController extends BaseController {

    @Autowired
    ReproductionService reproductionService;
    @Autowired
    MonthReportRecordService monthReportRecordService;
    @Autowired
    HarmlessReportService harmlessReportService;
    @Autowired
    QuarantineService quarantineService;
    @Autowired
    SlaughterRecordService slaughterRecordService;
    @Autowired
    EarMarkService earMarkService;
    @Autowired
    UserMenuService userMenuService;
    @Autowired
    UserService userService;
    @Autowired
    ProcessstayService processstayService;
    @Autowired
    ProcessListService processListService;
    @Autowired
    ReviewerService reviewerService;
    @Autowired
    ReportService reportService;
    @Autowired
    ProcessrecordService processrecordService;
    @Autowired
    private AnimalDailyService animalDailyService;
    @Autowired
    private FarmersDailyService farmersDailyService;
    @Autowired
    private PigAccountService pigAccountService;
    @Autowired
    private CertificateBService certificateBService;
    @Autowired
    private CertificateAService certificateAService;
    @Autowired
    private AnimalOriginService animalOriginService;
    @Autowired
    private BusinessAccountService businessAccountService;
    @Autowired
    private AnimalApplicationService animalApplicationService;
    @Autowired
    private InvestorsService investorsService;
    @Autowired
    private SaleDeclarationAService saleDeclarationAService;
    @Autowired
    private SaleDeclarationBService saleDeclarationBService;
    @Autowired
    private AnimalNumberService animalNumberService;
    @Autowired
    private ProductionService productionService;
    @Autowired
    private EntryRecordService entryRecordService;

    /**
     * 首页 index
     */
    @GetMapping("/index")
    @AuthToken
    public String index(Model model) {
        model.addAttribute("AnimalTypes",getLoginAnimalTypes());
        //获得当前用户的角色信息
        if ("1".equals(getUserType())){ //超级管理员
            return "index1";
        }
        if ("2".equals(getUserType())){ //执法人员
            return "index2";
        }
        if ("3".equals(getUserType())){ //养殖企业
            return "index3";
        } else if ("4".equals(getUserType())){ //屠宰企业
            return "index4";
        }else if ("5".equals(getUserType())){
            return "index5";
        }else if("6".equals(getUserType())){
            return "index6";
        }
        return null;
    }

    /**
     * 查询当前时间往后30天的预产期信息
     * @return
     */
    @PostMapping("/getPreProduction")
    @ResponseBody
    @AuthToken
    public ReturnVO getPreProduction(@RequestBody ReproductionVo reproductionVo){
        ReturnVO returnVO = new ReturnVO();
        Date startTime = new Date();
        Date endTime = CalendarDate.addDate(startTime,"day",30);
        QueryWrapper<Reproduction> wrapper = new QueryWrapper<>();
        wrapper.between("productionTime",startTime,endTime).eq("eid",getLoginEnterpriseId()).isNull("deliveryTime").eq("state",1);
        wrapper.orderByDesc("ProductionTime").orderByDesc("createTime");
        Page<Reproduction> page = new Page<Reproduction>(reproductionVo.getPageIndex(),reproductionVo.getPageSize());
        IPage<Reproduction> pageResult = reproductionService.page(page,wrapper);
        returnVO.setTotal((int)pageResult.getTotal());
        returnVO.setData(pageResult.getRecords());
        return returnVO;
    }

    /**
     * 分娩
     * @param reproduction
     * @return
     */
    @PostMapping("/savePreProduction")
    @ResponseBody
    @AuthToken
    public ReturnVO savePreProduction(@RequestBody Reproduction reproduction){
        ReturnVO result = new ReturnVO();
        try {
            if(reproductionService.updateById(reproduction)){
                result.setMessage("保存成功！");
                result.setCode("200");
            }else{
                result.setMessage("保存失败！");
                result.setCode("500");
            }
        }catch (Exception e){
            log.error(e.getMessage());
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }

    /**
     * 月报表记录
     * @return
     */
    @PostMapping("/getMonthlyReport")
    @ResponseBody
    @AuthToken
    public ReturnVO getMonthlyReport(){
        ReturnVO returnVO = new ReturnVO();

        List<MonthReportRecord> datalist = new ArrayList<>();
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//设置当前日期
        calendar.add(Calendar.MONTH, -1);//月份减一
        calendar.set(Calendar.DAY_OF_MONTH, 1);//第一天
//        System.out.println(dateTimeFormat.format(calendar.getTime()));//输出上个月的日期


        //查询上月报表
        QueryWrapper<MonthReportRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("reportMonth",dateTimeFormat.format(calendar.getTime())).eq("eid",getLoginEnterpriseId());
        List<MonthReportRecord> list = monthReportRecordService.list(wrapper);

        //查询表中是否存在记录
        QueryWrapper<MonthReportRecord> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("eid",getLoginEnterpriseId());
        int total = monthReportRecordService.count(wrapper1);
        if (list.size() <= 0 && total > 0){
            MonthReportRecord monthReportRecord1 = new MonthReportRecord();
            datalist.add(monthReportRecord1);
        }
        returnVO.setData(datalist);
        return returnVO;
    }

    /**
     * 待运输信息
     * @param harmlessReportVo
     * @return
     */
    @PostMapping("/getTransport")
    @ResponseBody
    @AuthToken
    public ReturnVO getTransport(@RequestBody HarmlessReportVo harmlessReportVo) {
        ReturnVO result = new ReturnVO();
        List<HarmlessReportVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<HarmlessReportVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            if (harmlessReportVo.getEnterpriseName() != null && !harmlessReportVo.getEnterpriseName().equals(""))
                queryWrapper.like("enterpriseName",harmlessReportVo.getEnterpriseName());
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=harmlessReportService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= harmlessReportService.search(queryWrapper,new Page<>(harmlessReportVo.getPageIndex(),harmlessReportVo.getPageSize()));
                if (datalist.size() > 0){
                    for (HarmlessReportVo harmlessReportVo1:datalist){
                        if (harmlessReportVo1.getImg() != null && !harmlessReportVo1.getImg().equals("")){
                            harmlessReportVo1.setImg(WebConfig.host+"images/"+harmlessReportVo1.getImg());
                        }
                    }
                }
            }
            result.setData(datalist);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }

    /**
     * 待处理信息
     * @param harmlessReportVo
     * @return
     */
    @PostMapping("/getHandle")
    @ResponseBody
    @AuthToken
    public ReturnVO getHandle(@RequestBody HarmlessReportVo harmlessReportVo) {
        ReturnVO result = new ReturnVO();
        List<HarmlessReportVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<HarmlessReportVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",2);
            if (harmlessReportVo.getEnterpriseName() != null && !harmlessReportVo.getEnterpriseName().equals(""))
                queryWrapper.like("enterpriseName",harmlessReportVo.getEnterpriseName());
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=harmlessReportService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= harmlessReportService.search(queryWrapper,new Page<>(harmlessReportVo.getPageIndex(),harmlessReportVo.getPageSize()));
                if (datalist.size() > 0){
                    for (HarmlessReportVo harmlessReportVo1:datalist){
                        if (harmlessReportVo1.getImg() != null && !harmlessReportVo1.getImg().equals("")){
                            harmlessReportVo1.setImg(WebConfig.host+"images/"+harmlessReportVo1.getImg());
                        }
                    }
                }
            }
            result.setData(datalist);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }

    /**
     * 待检疫信息
     * @param quarantineVo
     * @return
     */
    @PostMapping("/getQuarantine")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody QuarantineVo quarantineVo) {
        ReturnVO result = new ReturnVO();
        List<Quarantine> datalist=new ArrayList<>();
        try {
            QueryWrapper<Quarantine> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("state",0);
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=quarantineService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist=quarantineService.page(new Page<>(quarantineVo.getPageIndex(),quarantineVo.getPageSize()),queryWrapper).getRecords();
                for(Quarantine quarantine:datalist){
                    quarantine.setLivestockPhoto(WebConfig.host+"images/"+quarantine.getLivestockPhoto());
                    quarantine.setCarPhoto(WebConfig.host+"images/"+quarantine.getCarPhoto());
                }
            }
            result.setData(datalist);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }

    /**
     * 屠宰数据统计
     * @param
     * @return
     */
    @PostMapping("/dataStatistics")
    @ResponseBody
    @AuthToken
    public Map<String,Map<String,Object>> dataStatistics() {

        Map<String,Map<String,Object>> map = new HashMap<>();

        //日屠宰
        Map<String,Object> day = new HashMap<>();
        //待屠宰
        Map<String,Object> stayDie = new HashMap<>();
        //待出厂
        Map<String,Object> stayOut = new HashMap<>();

        //日屠宰种类
        int commodity = 0; //商品猪
        int eliminate = 0; //淘汰猪
        int cattle = 0; //牛
        int sheep = 0; //羊
        int poultry = 0; //家禽

        //待屠宰种类
        int commodity1 = 0; //商品猪
        int eliminate1 = 0; //淘汰猪
        int cattle1 = 0; //牛
        int sheep1 = 0; //羊
        int poultry1 = 0; //家禽

        //待出厂
        int commodity2 = 0; //商品猪
        int eliminate2 = 0; //淘汰猪
        int cattle2 = 0; //牛
        int sheep2 = 0; //羊
        int poultry2 = 0; //家禽

        //获得日屠宰信息
        QueryWrapper<SlaughterRecord> wrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.eq("eid",getLoginEnterpriseId());
        wrapper.eq("slaughterDate",simpleDateFormat.format(new Date()));
        List<SlaughterRecord> dayList = slaughterRecordService.list(wrapper);
        if (dayList.size() > 0){

            for (SlaughterRecord slaughterRecord:dayList){
                switch (slaughterRecord.getType()){
                    case "商品猪":
                        commodity += slaughterRecord.getSlaughterNumber();
                        break;
                    case "淘汰猪":
                        eliminate += slaughterRecord.getSlaughterNumber();
                        break;
                    case "牛":
                        cattle += slaughterRecord.getSlaughterNumber();
                        break;
                    case "羊":
                        sheep += slaughterRecord.getSlaughterNumber();
                        break;
                    case "家禽":
                        poultry += slaughterRecord.getSlaughterNumber();
                        break;
                }
            }
        }
        //日屠宰
        day.put("commodity",commodity);
        day.put("eliminate",eliminate);
        day.put("cattle",cattle);
        day.put("sheep",sheep);
        day.put("poultry",poultry);

        //获得待屠宰信息
        QueryWrapper<EntryRecord> entryRecordQueryWrapper = new QueryWrapper<>();
        entryRecordQueryWrapper.eq("eid",getLoginEnterpriseId());
        entryRecordQueryWrapper.gt("livestock",0);
        List<EntryRecord> entryRecordList = entryRecordService.list(entryRecordQueryWrapper);
        if (entryRecordList.size() > 0){
            for (EntryRecord entryRecord:entryRecordList){
                switch (entryRecord.getType()){
                    case "商品猪":
                        commodity1 += entryRecord.getLivestock();
                        break;
                    case "淘汰猪":
                        eliminate1 += entryRecord.getLivestock();
                        break;
                    case "牛":
                        cattle1 += entryRecord.getLivestock();
                        break;
                    case "羊":
                        sheep1 += entryRecord.getLivestock();
                        break;
                    case "家禽":
                        poultry1 += entryRecord.getLivestock();
                        break;
                }
            }
        }

        //待屠宰
        stayDie.put("commodity",commodity1);
        stayDie.put("eliminate",eliminate1);
        stayDie.put("cattle",cattle1);
        stayDie.put("sheep",sheep1);
        stayDie.put("poultry",poultry1);

        //获得待出厂数
        QueryWrapper<SlaughterRecord> slaughterRecordQueryWrapper = new QueryWrapper<>();
        slaughterRecordQueryWrapper.eq("eid",getLoginEnterpriseId());
        slaughterRecordQueryWrapper.eq("outState",0);
        List<SlaughterRecord> slaughterRecordList = slaughterRecordService.list(slaughterRecordQueryWrapper);
        if (slaughterRecordList.size() > 0){
            for (SlaughterRecord slaughterRecord:slaughterRecordList){
                switch (slaughterRecord.getType()){
                    case "商品猪":
                        commodity2 += slaughterRecord.getSlaughterNumber();
                        break;
                    case "淘汰猪":
                        eliminate2 += slaughterRecord.getSlaughterNumber();
                        break;
                    case "牛":
                        cattle2 += slaughterRecord.getSlaughterNumber();
                        break;
                    case "羊":
                        sheep2 += slaughterRecord.getSlaughterNumber();
                        break;
                    case "家禽":
                        poultry2 += slaughterRecord.getSlaughterNumber();
                        break;
                }
            }
        }

        //待出厂
        stayOut.put("commodity",commodity2);
        stayOut.put("eliminate",eliminate2);
        stayOut.put("cattle",cattle2);
        stayOut.put("sheep",sheep2);
        stayOut.put("poultry",poultry2);

        map.put("day",day);
        map.put("stayDie",stayDie);
        map.put("stayOut",stayOut);

        return map;
    }

    /**
     * 待屠宰提醒
     */
    @PostMapping("/slaughtered")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody EntryRecordVo entryRecordVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ReturnVO returnVO = new ReturnVO();
        List<EntryRecord> list = new ArrayList<>();
        QueryWrapper<EntryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eid",getLoginEnterpriseId());
        queryWrapper.gt("livestock",0);
        queryWrapper.orderByDesc("EntryDate");
        Page<EntryRecord> pageResult = entryRecordService.page(new Page<>(entryRecordVo.getPageIndex(),entryRecordVo.getPageSize()),queryWrapper);
        returnVO.setTotal((int)pageResult.getTotal());
        returnVO.setData(pageResult.getRecords());
        return returnVO;
    }

    /**
     * 待出厂提醒
     */
    @PostMapping("/exfactory")
    @ResponseBody
    public ReturnVO exfactory(@RequestBody SlaughterRecordVo slaughterRecordVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            ReturnVO result = new ReturnVO();
            List<SlaughterRecordVo> slaughterRecordVos=new ArrayList<>();
            QueryWrapper<SlaughterRecordVo> wrapper =new QueryWrapper<>();
            wrapper.eq("a.eid",getLoginEnterpriseId());
            wrapper.eq("outState",0);//未出厂
            wrapper.orderByDesc("entryDate");
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=slaughterRecordService.count(wrapper);
            //再查询
            if(total>=0)
            {
                slaughterRecordVos=slaughterRecordService.search(wrapper,new Page<>(slaughterRecordVo.getPageIndex(),slaughterRecordVo.getPageSize()));
            }
            result.setTotal(total);
            result.setData(slaughterRecordVos);
            return result;
    }

    /**
     * 根据菜单权限判断首页内容是否显示 0不显示，1显示
     */
    @PostMapping("/authority")
    @ResponseBody
    public Map<String,Integer> authority() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Map<String,Integer> map = new HashMap<>();

        if (getLoginGrade() == 1){ //一级账号
            return null;
        }

        //消息
        int newsusers = 0;
        //公告
        int noticeUsers = 0;
        //养殖无害化处理
        int harmlessReporting = 0;
        //预产期提醒
        int reproductionManage = 0;
        //月报填报提醒
        int monthReportRecord = 0;
        //待屠宰提醒
        int entryRecord = 0;
        //待出厂提醒
        int factoryRecord = 0;
        //待运输
        int harmlessReportTransport = 0;
        //待处理
        int harmlessReportPending = 0;

        //根据用户id查询到对应的菜单
        QueryWrapper<UserMenuVo> wrapper = new QueryWrapper<>();
        wrapper.eq("um.UserId",getLoginUserId());
        List<UserMenuVo> list = userMenuService.selectUserMenuList(wrapper);
        for (UserMenuVo userMenuVo:list){
            if ("newsusers".equals(userMenuVo.getKeyName())){
                newsusers = 1;
            }
            if ("noticeUsers".equals(userMenuVo.getKeyName())){
                noticeUsers = 1;
            }
            if ("HarmlessReporting".equals(userMenuVo.getKeyName())){
                harmlessReporting = 1;
            }
            if ("reproductionManage".equals(userMenuVo.getKeyName())){
                reproductionManage = 1;
            }
            if ("monthReportRecord".equals(userMenuVo.getKeyName())){
                monthReportRecord = 1;
            }
            if ("entryRecord".equals(userMenuVo.getKeyName())){
                entryRecord = 1;
            }
            if ("factoryRecord".equals(userMenuVo.getKeyName())){
                factoryRecord = 1;
            }
            if ("harmlessReportTransport".equals(userMenuVo.getKeyName())){
                harmlessReportTransport = 1;
            }
            if ("harmlessReportPending".equals(userMenuVo.getKeyName())){
                harmlessReportPending = 1;
            }
        }

        map.put("newsusers",newsusers);
        map.put("noticeUsers",noticeUsers);
        map.put("harmlessReporting",harmlessReporting);
        map.put("reproductionManage",reproductionManage);
        map.put("monthReportRecord",monthReportRecord);
        map.put("entryRecord",entryRecord);
        map.put("factoryRecord",factoryRecord);
        map.put("harmlessReportTransport",harmlessReportTransport);
        map.put("harmlessReportPending",harmlessReportPending);

        return map;
    }

//    /**
//     * 根据部门id获得审批人
//     * @return
//     */
//    @PostMapping("/getUserByDept")
//    @ResponseBody
//    @AuthToken
//    public List<Users> getUserByDept(@Param("deptId") Integer deptId){
//        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("deptId",deptId);
//        queryWrapper.eq("activity",1);
//        //只能向下驳回
//        //查询当前节点下的所有审批人的Id
//
//        List<Users> list = userService.list(queryWrapper);
//        return list;
//    }

    /**
     * 审批
     * @return
     */
    @PostMapping("/approval")
    @ResponseBody
    @AuthToken
    public ReturnVO approval(@RequestBody ProcessrecordVo processrecordVo){
        ReturnVO returnVO = new ReturnVO();
        //获得待审记录
        Processstay processstay = processstayService.getById(processrecordVo.getStyId());
        if (processstay == null){
            returnVO.setCode("500");
            return returnVO;
        }

        //获得当前节点
        ProcessList processList = processListService.getById(processstay.getProcesslistId());
        if (processList == null){
            returnVO.setCode("500");
            return returnVO;
        }
        //获得当前审批人
        Reviewer reviewer = reviewerService.getById(processstay.getReviewerId());
        if (reviewer == null){
            returnVO.setCode("500");
            return returnVO;
        }
        //选择
        if (processrecordVo.getStatus() == null){
            returnVO.setCode("500");
            return returnVO;
        }

        //获得审批最大顺序
        int orderId;
        QueryWrapper<Processrecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form",processstay.getForm());
        queryWrapper.eq("formid",processstay.getFormId());
        queryWrapper.orderByDesc("orderId");
        List<Processrecord> list = processrecordService.list(queryWrapper);
        if (list.size() > 0){
            orderId = list.get(0).getOrderId() + 1;
        }else {
            orderId = 1;
        }

        //创建审批记录
        Processrecord processrecord = new Processrecord();
        if (processrecordVo.getStatus() == -1){//驳回
            processrecord.setStatus(Constants.REJECT);
        }else {
            processrecord.setStatus(processrecordVo.getStatus());
        }
        processrecord.setLaunchTime(processstay.getLaunchTime());
        processrecord.setLaunchId(processstay.getLaunchId());
        processrecord.setReviewerId(processstay.getReviewerId());
        processrecord.setFormId(processstay.getFormId());
        processrecord.setForm(processstay.getForm());
        processrecord.setProcessId(processstay.getProcessId());
        processrecord.setReviewerTime(new Date());
        processrecord.setUserId(processstay.getUserId());
        processrecord.setProcesslistId(processstay.getProcesslistId());
        processrecord.setOrderId(orderId);
        processrecordService.save(processrecord);

        //驳回给发起人
        if (processrecordVo.getStatus() == -1){//驳回给发起人
            //删除待审记录
            QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
            wrapper.eq("Form",processstay.getForm());
            wrapper.eq("formId",processstay.getFormId());
            processstayService.remove(wrapper);
            //判断表单
//            if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                //修改对应表单信息
//                Report report = new Report();
//                report.setId(processstay.getFormId());
//                report.setTypes(Constants.REJECT);//驳回
//                reportService.updateById(report);
//            }
            updateForm(processstay,Constants.REJECT);
        }else if (processrecordVo.getStatus() == 3){ //驳回给选择的节点
            //删除待审记录
            QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
            wrapper.eq("Form",processstay.getForm());
            wrapper.eq("formId",processstay.getFormId());
            processstayService.remove(wrapper);
            //创建新的待审记录
            Integer processId = ActivitiUtil.getProcessIdByForm(processstay.getForm());
            if (processId == null)
                throw new RuntimeException("该流程不存在！");
            //获得下一个审批节点
            ProcessList processList1 = ActivitiUtil.getProcessList(processId,0);
            if (processList1 == null)
                throw new RuntimeException("审批不存在！");
            if (processList1.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
                //获得下一个审批人
                Reviewer reviewer1 = ActivitiUtil.getReviewerNext(processList1.getId(),0);
                if (reviewer == null)
                    throw new RuntimeException("审批人不存在！");
                //创建待审记录
                Processstay processstay1 = new Processstay();
                processstay1.setProcessId(processId);
                processstay1.setForm(processstay.getForm());
                processstay1.setFormId(processstay.getFormId());
                processstay1.setProcesslistId(processList1.getId());
                processstay1.setTypes(processList1.getTypes());
                processstay1.setReviewerId(reviewer1.getId());
                processstay1.setUserId(reviewer1.getReviewer());
                processstay1.setLaunchId(processstay.getLaunchId());
                processstay1.setLaunchTime(processstay.getLaunchTime());
                processstayService.save(processstay1);
            }else { //会签或或签
                //获得下一批审批人
                List<Reviewer> list1 = ActivitiUtil.getReviewer(processList1.getId());
                if (list1 == null)
                    throw new RuntimeException("审批人不存在！");
                for (Reviewer reviewer1:list1){
                    //创建待审记录
                    Processstay processstay1 = new Processstay();
                    processstay1.setProcessId(processId);
                    processstay1.setForm(processstay.getForm());
                    processstay1.setFormId(processstay.getFormId());
                    processstay1.setProcesslistId(processList1.getId());
                    processstay1.setTypes(processList1.getTypes());
                    processstay1.setReviewerId(reviewer1.getId());
                    processstay1.setUserId(reviewer1.getReviewer());
                    processstay1.setLaunchId(processstay.getLaunchId());
                    processstay1.setLaunchTime(processstay.getLaunchTime());
                    processstayService.save(processstay1);
                }
            }
        }
        //判断
        if (processstay.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
            if (processrecordVo.getStatus().equals(Constants.FAILED)){ //审批未通过，结束流程
                //删除待审记录
                QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
                wrapper.eq("Form",processstay.getForm());
                wrapper.eq("formId",processstay.getFormId());
                processstayService.remove(wrapper);
                //判断表单
//                if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                    //修改对应表单信息
//                    Report report = new Report();
//                    report.setId(processstay.getFormId());
//                    report.setTypes(Constants.FAILED);//未通过
//                    reportService.updateById(report);
//                }
                updateForm(processstay,Constants.FAILED);
            }else if (processrecordVo.getStatus().equals(Constants.ADOPT)){ //审批通过
                //删除自身记录
                QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
                wrapper.eq("Form",processstay.getForm());
                wrapper.eq("formId",processstay.getFormId());
                wrapper.eq("userId",processstay.getUserId());
                processstayService.remove(wrapper);
                //获得下一个审批人
                Reviewer nextReviewer = ActivitiUtil.getReviewerNext(processList.getId(),reviewer.getOrderId());
                if (nextReviewer != null){ //下一个审批人存在
                    //创建待审记录
                    processstay.setReviewerId(nextReviewer.getId());
                    processstay.setUserId(nextReviewer.getReviewer());
                    processstayService.save(processstay);
                }else { //审批人不存在，寻找下一个节点
                    //获得下一个审批节点
                    ProcessList nextProcessList = ActivitiUtil.getProcessList(processList.getProcessId(),processList.getSort());
                    if (nextProcessList != null){ //下一个审批节点存在
                        if (nextProcessList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
                            //获得下一个审批人
                            Reviewer nextreviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
                            //看审批人是否存在
                            if (nextreviewer == null){ //审批人不存在
                                returnVO.setCode("501"); //审批人不存在
                                returnVO.setMessage("审批人不存在！");
                                return returnVO;
                            }else { //审批人存在
                                //创建待审记录
                                processstay.setProcesslistId(nextProcessList.getId());
                                processstay.setTypes(nextProcessList.getTypes());
                                processstay.setReviewerId(nextreviewer.getId());
                                processstay.setUserId(nextreviewer.getReviewer());
                                processstayService.save(processstay);
                            }
                        }else { //会签或或签
                            //获得下一批审批人
                            List<Reviewer> nextReviewerList = ActivitiUtil.getReviewer(nextProcessList.getId());
                            if (nextReviewerList == null){
                                returnVO.setCode("501"); //审批人不存在
                                returnVO.setMessage("审批人不存在！");
                                return returnVO;
                            }
                            for (Reviewer reviewer1:nextReviewerList){
                                //创建待审记录
                                processstay.setProcesslistId(nextProcessList.getId());
                                processstay.setTypes(nextProcessList.getTypes());
                                processstay.setReviewerId(reviewer1.getId());
                                processstay.setUserId(reviewer1.getReviewer());
                                processstayService.save(processstay);
                            }
                        }
                    }else { //下一个审批节点不存在，审批通过
                        //判断表单
//                        if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                            //修改对应表单信息
//                            Report report = new Report();
//                            report.setId(processstay.getFormId());
//                            report.setTypes(Constants.ADOPT);//通过
//                            reportService.updateById(report);
//                        }
                        updateForm(processstay,Constants.ADOPT);
                    }
                }
            }
        }else if (processstay.getTypes().equals(Constants.ORSIGN)){ //或签
            if (processrecordVo.getStatus().equals(Constants.FAILED)){ //审批未通过，结束流程
                //删除待审记录
                QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
                wrapper.eq("Form",processstay.getForm());
                wrapper.eq("formId",processstay.getFormId());
                processstayService.remove(wrapper);
                //判断表单
//                if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                    //修改对应表单信息
//                    Report report = new Report();
//                    report.setId(processstay.getFormId());
//                    report.setTypes(Constants.FAILED);//未通过
//                    reportService.updateById(report);
//                }
                updateForm(processstay,Constants.FAILED);
            }else if (processrecordVo.getStatus().equals(Constants.ADOPT)){ //审批通过
                //删除待审记录
                QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
                wrapper.eq("Form",processstay.getForm());
                wrapper.eq("formId",processstay.getFormId());
                processstayService.remove(wrapper);
                //获得下一个审批节点
                ProcessList nextProcessList = ActivitiUtil.getProcessList(processList.getProcessId(),processList.getSort());
                if (nextProcessList != null){ //下一个审批节点存在
                    if (nextProcessList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
                        //获得下一个审批人
                        Reviewer nextreviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
                        //看审批人是否存在
                        if (nextreviewer == null){ //审批人不存在
                            returnVO.setCode("501"); //审批人不存在
                            returnVO.setMessage("审批人不存在！");
                            return returnVO;
                        }else { //审批人存在
                            //创建待审记录
                            processstay.setProcesslistId(nextProcessList.getId());
                            processstay.setTypes(nextProcessList.getTypes());
                            processstay.setReviewerId(nextreviewer.getId());
                            processstay.setUserId(nextreviewer.getReviewer());
                            processstayService.save(processstay);
                        }
                    }else { //会签或或签
                        //获得下一批审批人
                        List<Reviewer> nextReviewerList = ActivitiUtil.getReviewer(nextProcessList.getId());
                        if (nextReviewerList == null){
                            returnVO.setCode("501"); //审批人不存在
                            returnVO.setMessage("审批人不存在！");
                            return returnVO;
                        }
                        for (Reviewer reviewer1:nextReviewerList){
                            //创建待审记录
                            processstay.setProcesslistId(nextProcessList.getId());
                            processstay.setTypes(nextProcessList.getTypes());
                            processstay.setReviewerId(reviewer1.getId());
                            processstay.setUserId(reviewer1.getReviewer());
                            processstayService.save(processstay);
                        }
                    }
                }else { //下一个审批节点不存在，审批通过
                    //判断表单
//                    if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                        //修改对应表单信息
//                        Report report = new Report();
//                        report.setId(processstay.getFormId());
//                        report.setTypes(Constants.ADOPT);//通过
//                        reportService.updateById(report);
//                    }
                    updateForm(processstay,Constants.ADOPT);
                }
            }
        }else if (processstay.getTypes().equals(Constants.JOINTLYSIGN)){ //会签
            //删除待审记录
            QueryWrapper<Processstay> wrapper = new QueryWrapper<>();
            wrapper.eq("Form",processstay.getForm());
            wrapper.eq("formId",processstay.getFormId());
            wrapper.eq("userId",processstay.getUserId());
            processstayService.remove(wrapper);
            //获得当前节点下审批人总数
            QueryWrapper<Reviewer> reviewerQueryWrapper = new QueryWrapper<>();
            reviewerQueryWrapper.eq("processListId",processstay.getProcesslistId());
            int reviewersum = reviewerService.count(reviewerQueryWrapper);
            //获得当前节点审批记录总数
            QueryWrapper<Processrecord> processrecordQueryWrapper = new QueryWrapper<>();
            processrecordQueryWrapper.eq("form",processstay.getForm());
            processrecordQueryWrapper.eq("formid",processstay.getFormId());
            processrecordQueryWrapper.eq("processListId",processstay.getProcesslistId());
            int processrecordsum = processrecordService.count(processrecordQueryWrapper);
            if (reviewersum == processrecordsum){ //所有人都审批完了
                //判断审批成功数是否与节点总人数相等
                processrecordQueryWrapper.eq("status",Constants.ADOPT);
                int successsum = processrecordService.count(processrecordQueryWrapper);
                if (reviewersum == successsum){ //会签成功，开始下一步流程
                    //获得下一个审批节点
                    ProcessList nextProcessList = ActivitiUtil.getProcessList(processList.getProcessId(),processList.getSort());
                    if (nextProcessList != null){ //下一个审批节点存在
                        if (nextProcessList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
                            //获得下一个审批人
                            Reviewer nextreviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
                            //看审批人是否存在
                            if (nextreviewer == null){ //审批人不存在
                                returnVO.setCode("501"); //审批人不存在
                                returnVO.setMessage("审批人不存在！");
                                return returnVO;
                            }else { //审批人存在
                                //创建待审记录
                                processstay.setProcesslistId(nextProcessList.getId());
                                processstay.setTypes(nextProcessList.getTypes());
                                processstay.setReviewerId(nextreviewer.getId());
                                processstay.setUserId(nextreviewer.getReviewer());
                                processstayService.save(processstay);
                            }
                        }else { //会签或或签
                            //获得下一批审批人
                            List<Reviewer> nextReviewerList = ActivitiUtil.getReviewer(nextProcessList.getId());
                            if (nextReviewerList == null){
                                returnVO.setCode("501"); //审批人不存在
                                returnVO.setMessage("审批人不存在！");
                                return returnVO;
                            }
                            for (Reviewer reviewer1:nextReviewerList){
                                //创建待审记录
                                processstay.setProcesslistId(nextProcessList.getId());
                                processstay.setTypes(nextProcessList.getTypes());
                                processstay.setReviewerId(reviewer1.getId());
                                processstay.setUserId(reviewer1.getReviewer());
                                processstayService.save(processstay);
                            }
                        }
                    }else { //下一个审批节点不存在，审批通过
                        //判断表单
//                        if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                            //修改对应表单信息
//                            Report report = new Report();
//                            report.setId(processstay.getFormId());
//                            report.setTypes(Constants.ADOPT);//通过
//                            reportService.updateById(report);
//                        }
                        updateForm(processstay,Constants.ADOPT);
                    }
                }else { //审批失败结束流程
                    //判断表单
//                    if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
//                        //修改对应表单信息
//                        Report report = new Report();
//                        report.setId(processstay.getFormId());
//                        report.setTypes(Constants.FAILED);//未通过
//                        reportService.updateById(report);
//                    }
                    updateForm(processstay,Constants.FAILED);
                }
            }
        }

        return returnVO;
    }

    /**
     * 修改对应表单
     * @param processstay
     * @param types
     */
    public void updateForm(Processstay processstay,Integer types){
        //修改对应表单信息
        if (processstay.getForm().equals(Constants.REPORT)){ //投诉举报
            Report report = new Report();
            report.setId(processstay.getFormId());
            report.setTypes(types);
            reportService.updateById(report);
        }else if (processstay.getForm().equals(Constants.ANIMALDAILY)){
            AnimalDaily animalDaily = new AnimalDaily();
            animalDaily.setId(processstay.getFormId());
            animalDaily.setTypes(types);
            animalDailyService.updateById(animalDaily);
        }else if (processstay.getForm().equals(Constants.FARMERSDAILY)){
            FarmersDaily farmersDaily = new FarmersDaily();
            farmersDaily.setId(processstay.getFormId());
            farmersDaily.setTypes(types);
            farmersDailyService.updateById(farmersDaily);
        }else if (processstay.getForm().equals(Constants.PIGACCOUNT)){
            PigAccount pigAccount = new PigAccount();
            pigAccount.setId(processstay.getFormId());
            pigAccount.setTypes(types);
            pigAccountService.updateById(pigAccount);
        }else if (processstay.getForm().equals(Constants.CERTIFICATEB)){
            CertificateB certificateB = new CertificateB();
            certificateB.setId(processstay.getFormId());
            certificateB.setTypes(types);
            certificateBService.updateById(certificateB);
        }else if (processstay.getForm().equals(Constants.CERTIFICATEA)){
            CertificateA certificateA = new CertificateA();
            certificateA.setId(processstay.getFormId());
            certificateA.setTypes(types);
            certificateAService.updateById(certificateA);
        }else if (processstay.getForm().equals(Constants.ANIMALORIGIN)){
            AnimalOrigin animalOrigin = new AnimalOrigin();
            animalOrigin.setId(processstay.getFormId());
            animalOrigin.setTypes(types);
            animalOriginService.updateById(animalOrigin);
        }else if (processstay.getForm().equals(Constants.BUSINESSACCOUNT)){
            BusinessAccount businessAccount = new BusinessAccount();
            businessAccount.setId(processstay.getFormId());
            businessAccount.setTypes(types);
            businessAccountService.updateById(businessAccount);
        }else if (processstay.getForm().equals(Constants.ANIMALAPPLICATION)){
            AnimalApplication animalApplication = new AnimalApplication();
            animalApplication.setId(processstay.getFormId());
            animalApplication.setTypes(types);
            animalApplicationService.updateById(animalApplication);
        }else if (processstay.getForm().equals(Constants.INVESTORS)){
            Investors investors = new Investors();
            investors.setId(processstay.getFormId());
            investors.setTypes(types);
            investorsService.updateById(investors);
        }else if (processstay.getForm().equals(Constants.SALEDECLARATIONA)){
            SaleDeclarationA saleDeclarationA = new SaleDeclarationA();
            saleDeclarationA.setId(processstay.getFormId());
            saleDeclarationA.setTypes(types);
            saleDeclarationAService.updateById(saleDeclarationA);
            SaleDeclarationA saleDeclarationA1=saleDeclarationAService.getById(processstay.getFormId());
            UpdateWrapper<AnimalNumber> animalNumberUpdateWrapper=new UpdateWrapper<>();
            AnimalNumber animalNumber=animalNumberService.getById(saleDeclarationA1.getAid());   //旧明细
            if(types==1){       //审批通过
                //修改动物明细记录
                if(saleDeclarationA1.getSaleAnimalType().equals("种公畜")){
                    animalNumberUpdateWrapper.set("species",animalNumber.getSpecies()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("后备母畜")){
                    animalNumberUpdateWrapper.set("reserve",animalNumber.getReserve()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("能繁母畜")){
                    animalNumberUpdateWrapper.set("menstruation",animalNumber.getMenstruation()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("哺乳牲畜")){
                    animalNumberUpdateWrapper.set("lactation",animalNumber.getLactation()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("保育牲畜")){
                    animalNumberUpdateWrapper.set("conservation",animalNumber.getConservation()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("育成牲畜")){
                    animalNumberUpdateWrapper.set("commodity",animalNumber.getCommodity()-saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("存栏家禽")){
                    animalNumberUpdateWrapper.set("livestock",animalNumber.getLivestock()-saleDeclarationA1.getNumber());
                }
                animalNumberUpdateWrapper.eq("id",saleDeclarationA1.getAid());
                animalNumberService.update(animalNumberUpdateWrapper);
                //新增一条进厂记录
                Production production=new Production();
                production.setEid(saleDeclarationA1.getEid());
                production.setChangeTime(saleDeclarationA1.getSaleDate());
                production.setSituation("调出");
                production.setAnimalType(saleDeclarationA1.getAnimalType());
                if(saleDeclarationA1.getSaleAnimalType().equals("种公畜")){
                    production.setMale(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("后备母畜")){
                    production.setFemale(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("能繁母畜")){
                    production.setBreedFemale(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("哺乳牲畜")){
                    production.setLactation(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("保育牲畜")){
                    production.setConservation(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("育成牲畜")){
                    production.setGrow(saleDeclarationA1.getNumber());
                }else if(saleDeclarationA1.getSaleAnimalType().equals("存栏家禽")){
                    production.setNumber(saleDeclarationA1.getNumber());
                }
                production.setCreateTime(new Date());
                productionService.save(production);
            }
        }else if (processstay.getForm().equals(Constants.SALEDECLARATIONB)){
            SaleDeclarationB saleDeclarationB = new SaleDeclarationB();
            saleDeclarationB.setId(processstay.getFormId());
            saleDeclarationB.setTypes(types);
            saleDeclarationBService.updateById(saleDeclarationB);
            SaleDeclarationB saleDeclarationB1=saleDeclarationBService.getById(processstay.getFormId());
            UpdateWrapper<AnimalNumber> animalNumberUpdateWrapper=new UpdateWrapper<>();
            AnimalNumber animalNumber=animalNumberService.getById(saleDeclarationB1.getAid());   //旧明细
            if(types==1){       //审批通过
                //修改动物明细记录
                if(saleDeclarationB1.getSaleAnimalType().equals("种公畜")){
                    animalNumberUpdateWrapper.set("species",animalNumber.getSpecies()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("后备母畜")){
                    animalNumberUpdateWrapper.set("reserve",animalNumber.getReserve()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("能繁母畜")){
                    animalNumberUpdateWrapper.set("menstruation",animalNumber.getMenstruation()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("哺乳牲畜")){
                    animalNumberUpdateWrapper.set("lactation",animalNumber.getLactation()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("保育牲畜")){
                    animalNumberUpdateWrapper.set("conservation",animalNumber.getConservation()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("育成牲畜")){
                    animalNumberUpdateWrapper.set("commodity",animalNumber.getCommodity()-saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("存栏家禽")){
                    animalNumberUpdateWrapper.set("livestock",animalNumber.getLivestock()-saleDeclarationB1.getNumber());
                }
                animalNumberUpdateWrapper.eq("id",saleDeclarationB1.getAid());
                animalNumberService.update(animalNumberUpdateWrapper);
                //新增一条进厂记录
                Production production=new Production();
                production.setEid(saleDeclarationB1.getEid());
                production.setChangeTime(saleDeclarationB1.getSaleDate());
                production.setSituation("调出");
                production.setAnimalType(saleDeclarationB1.getAnimalType());
                if(saleDeclarationB1.getSaleAnimalType().equals("种公畜")){
                    production.setMale(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("后备母畜")){
                    production.setFemale(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("能繁母畜")){
                    production.setBreedFemale(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("哺乳牲畜")){
                    production.setLactation(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("保育牲畜")){
                    production.setConservation(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("育成牲畜")){
                    production.setGrow(saleDeclarationB1.getNumber());
                }else if(saleDeclarationB1.getSaleAnimalType().equals("存栏家禽")){
                    production.setNumber(saleDeclarationB1.getNumber());
                }
                production.setCreateTime(new Date());
                productionService.save(production);
            }
        }
    }

    /**
     * 获得自身节点下所有节点
     * @return
     */
    @PostMapping("/getReviewer")
    @ResponseBody
    @AuthToken
    public List<ReviewerVo> getReviewer(@RequestBody ReviewerVo reviewerVo){
        List<ReviewerVo> datalist = new ArrayList<>();
        //获得审批人
        Reviewer reviewer = reviewerService.getById(reviewerVo.getId());
        List<Integer> listReviewerid = new ArrayList<>();
        //判断审批方式
        if (reviewerVo.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
            //获得下面的审批人
            QueryWrapper<Reviewer> wrapper = new QueryWrapper<>();
            wrapper.eq("processListId",reviewer.getProcessListId());
            wrapper.lt("orderId",reviewer.getOrderId());
            List<Reviewer> list = reviewerService.list(wrapper);
            if (list.size() > 0){
                for (Reviewer reviewer1:list){
                    listReviewerid.add(reviewer1.getId());
                }
            }
        }
        //获得下面所有节点的审批人
        ProcessList processList = processListService.getById(reviewer.getProcessListId());
        QueryWrapper<ProcessList> listQueryWrapper = new QueryWrapper<>();
        listQueryWrapper.eq("processId",processList.getProcessId());
        listQueryWrapper.lt("sort",processList.getSort());
        List<ProcessList> processrecordList = processListService.list(listQueryWrapper);
        if (processrecordList.size() > 0){
            for (ProcessList processList1:processrecordList){
                //根据节点id获得下面的审批人
                List<Reviewer> reviewerList = ActivitiUtil.getReviewer(processList1.getId());
                if (reviewerList.size() > 0){
                    for (Reviewer reviewer1:reviewerList){
                        listReviewerid.add(reviewer1.getId());
                    }
                }
            }
        }
        //获得审批人集合
        QueryWrapper<ReviewerVo> wrapper = new QueryWrapper<>();
        if (listReviewerid.size() > 0){
            wrapper.in("id",listReviewerid);
        }else {
            wrapper.eq("id",0);
        }
        wrapper.eq("deptid",reviewerVo.getDeptid());
        datalist = reviewerService.search(wrapper,new Page<ReviewerVo>(1,9999));
        return datalist;
    }
}
